package de.tabuma.ssoclient.logik;

import java.util.*;
import java.util.logging.*;
import de.tabuma.ssoclient.entity.Group;

/**
 *
 * @author alefen
 */
public class GroupHashFacade {

    private static Logger log = Logger.getLogger(GroupHashFacade.class.getName());

    private static Hashtable<String, List<Group>> hash = new Hashtable<String, List<Group>>();
    public synchronized static void put(Group group, String ssoId) {
        List<Group> gList = hash.get(ssoId);
        if(gList==null) {
            gList = new ArrayList<Group>();
        }
        for(int i=0;i<gList.size();i++) {
            Group groupListElement = gList.get(i);
            if(groupListElement.getId().intValue()==group.getId().intValue()) {
                gList.set(i, group);
                hash.put(ssoId, gList);
                return;
            }
        }
        gList.add(group);
        hash.put(ssoId, gList);
        return;
    }

    public synchronized static Group get(int id, String ssoId) {
        List<Group> gList = hash.get(ssoId);
        if(gList==null) {
            return null;
        }
        for(int i=0;i<gList.size();i++) {
            Group g = gList.get(i);
            if(g.getId().intValue()==id) {
                return g;
            }
        }
        return null;
    }

    public synchronized static void remove(int id, String ssoId) {
        List<Group> gList = hash.get(ssoId);
        if(gList==null) {
            return;
        }
        for(int i=0;i<gList.size();i++) {
            Group groupListElement = gList.get(i);
            if(groupListElement.getId().intValue()==id) {
                gList.remove(i);
                hash.put(ssoId, gList);
                return;
            }
        }
    }

    public synchronized static List<Group> getList(String ssoId) {
        return hash.get(ssoId);
    }

    public synchronized static void addList(List<Group> gList, String ssoId) {
        hash.put(ssoId, gList);
    }
}